﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Holiday;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Holiday
{
    public class AddHolidayCommandHarder : IRequestHandler<AddHolidayCommand, APIResult<object>>
    {
        private readonly IBaseRepository<WF_Recode> repository;
        private readonly IBaseRepository<Holidays> menuRep;
        private readonly IMapper mapper;

        public AddHolidayCommandHarder(IBaseRepository<WF_Recode> repository, IBaseRepository<Holidays> menuRep, IMapper mapper)
        {
            this.repository = repository;
            this.menuRep = menuRep;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(AddHolidayCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var model=mapper.Map<Holidays>(request);
            model.Spzt= Approval.已提交.ToString();

            model.HoTime = DateTime.Now;
            model.CreateTime = DateTime.Now;
            model.yj = "";


            model.Del=false;
            var res = menuRep.Add(model).Result;
          WF_Recode recode = new WF_Recode();
            recode.SqrId= request.UserId;
            recode.SqrrName = model.HoName;
            recode.SqrrYuyin = model.Holyue;
            recode.UserId= request.UserId;
            recode.UserName = request.UserName;
            recode.YiJian = "";
            recode.zt = 1;
            recode.CreateTime=DateTime.Now;
            recode.approvalId= request.approvalId;
            recode.HoId= model.HoId;
            repository.Add(recode); 
            if (res > 0)
            {

                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res;
            }

            return Task.FromResult(result);
        }
    }
}  
